package edu.ybu.manage.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import edu.ybu.manage.mapper.ProductMapper;
import edu.ybu.manage.pojo.Product;
import edu.ybu.manage.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ProductServiceImpl implements ProductService {

    @Autowired
    private ProductMapper productMapper;
    @Override
    public List<Product> selectAllProduct() {
        return productMapper.selectList(null);
    }

    @Override
    public Product selectProductById(Product product) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("id",product.getId());
        return productMapper.selectById(queryWrapper);
    }

    @Override
//    更新
    public Integer updateProductById(Product product) {
        return productMapper.updateById(product);
    }

    @Override
    public Integer getCount() {

        return productMapper.getProductCount();
    }

    @Override
//    分页搜索
    public List<Product> searchProductsByPage(Integer page, Integer size, Product product) {
        List<Product> products = productMapper.searchTeachersByPage((page-1)*size,size,product.getProductname());
        return products;
    }

    @Override
//    添加
    public Integer insertProduct(Product product) {
        return productMapper.insert(product);
    }

    @Override
//    删除
    public Integer deleteProduct(Product product) {
        return productMapper.deleteById(product.getId());
    }

    @Override
//    分页查询
    public List<Product> selectAllProductByPage(Integer page, Integer size) {
        List<Product> products = productMapper.selectAllProductByPage((page-1)*size,size);
//        System.out.println(products);
        return products;
    }

    @Override
    public int getSearchCount(Product product) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.like("product_name",product.getProductname());
        return productMapper.selectCount(queryWrapper);
    }

    @Override
    public Integer deleteProducts(List<Integer> integers) {

        return productMapper.deleteBatchIds(integers);
    }
}
